#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stropts.h>
#include<linux/fb.h>
#include <sys/mman.h>

#define LCD "/dev/ubuntu_lcd"
typedef unsigned int* lcd;
typedef unsigned int uint;

int size=800*480*4;
int lcdfd=-1;

lcd open_lcd(){
    lcdfd = open(LCD,O_RDWR);
    if(-1 == lcdfd){
        perror("open err");
        return NULL;
    }
    printf("设备打开成功\n");

    // struct fb_var_screeninfo info;
    // int ret = ioctl(lcdfd,FBIOGET_VSCREENINFO,&info);

    // if(-1 == ret){
    //     perror("获取设备信息失败");
    //     return NULL;
    // }

    // size=info.xres_virtual*info.yres_virtual*(info.bits_per_pixel/8);

    lcd p=mmap(NULL,size,PROT_READ | PROT_WRITE,MAP_SHARED,lcdfd,0);
    if(MAP_FAILED == p){
        perror("映射内存失败");
        return NULL;
    }

    printf("映射成功\n");
    return p;
}

void view(int start,int end,lcd p,uint color){
    for(int i =start;i < end;i++){
        p[i]=color;
    }
}

int close_lcd(lcd p){

    int ret = munmap(p,size);
    if(-1 == ret){
        perror("解除映射失败");
    }
    printf("解除映射成功\n");
    ret = close(lcdfd);
    if(-1 == ret){
        perror("关闭设备失败\n");
        return -1;
    }
    printf("设备已关闭\n");
    return 0;

}

int main(int argc,char const argv[]){

    lcd p=open_lcd();

    unsigned int blue=0x000000ff;
    unsigned int green=0x0000ff00;
    unsigned int red=0x00ff0000;

    view(0,size/12,p,blue);//前三分之一为蓝
    view(size/12,size/6,p,blue+green+red);
    view(size/6,size/4,p,red);




    close_lcd(p);

    return 0;

}